package com.cjh.wechat.core.controller;

import java.util.Iterator;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.cjh.wechat.commom.pojo.Group;
import com.cjh.wechat.commom.pojo.Notify;
import com.cjh.wechat.commom.pojo.User;
import com.cjh.wechat.commom.type.ApplyStatus;
import com.cjh.wechat.commom.vo.PageVo;
import com.cjh.wechat.core.service.GroupService;
import com.cjh.wechat.core.service.NotifyService;
import com.cjh.wechat.core.service.UserService;

@Controller
@RequestMapping("/notify")
public class NotifyController
{
    @Resource
    private NotifyService    notifyService;
    @Resource
    private UserService      userService;
    @Resource
    private GroupService     groupService;
    private static final int PAGE_SIZE = 10;
    
    @ResponseBody
    @RequestMapping(value = "/findByUid", method = RequestMethod.POST)
    public PageVo findById(String uid, int page)
    {
        List<Notify> notifies = notifyService.findByUid(uid, page, PAGE_SIZE);
        Iterator<Notify> iterator = notifies.iterator();
        while (iterator.hasNext())
        {
            Notify notify = iterator.next();
            if (notify.getHref() != null)
            {
                Group group = groupService.getByGid(notify.getFromUid());
                notify.setGroup(group);
            }
            else
            {
                User user = userService.findById(notify.getFromUid());
                notify.setUser(user);
            }
        }
        int total = notifyService.findByUidSum(uid);
        return buildPageVo(total, notifies);
    }
    
    @ResponseBody
    @RequestMapping(value = "/getUnreadMsgNum", method = RequestMethod.POST)
    public int getUnreadMsgNum(String uid)
    {
        int unreadNum = notifyService.getUnreadMsgNum(uid);
        return unreadNum;
    }
    
    @ResponseBody
    @RequestMapping(value = "/updateByUid", method = RequestMethod.POST)
    public String updateByUid(@ModelAttribute Notify notify)
    {
        notifyService.updateByUidSelective(notify);
        return "success";
    }
    
    @ResponseBody
    @RequestMapping(value = "/agree", method = RequestMethod.POST)
    public String agree(Integer no, Integer tno)
    {
        try
        {
            notifyService.agreeApply(no, tno);
            return "success";
        }
        catch (Exception e)
        {
            return "fail";
        }
    }
    
    @ResponseBody
    @RequestMapping(value = "/refuse", method = RequestMethod.POST)
    public String refuse(Integer no)
    {
        try
        {
            notifyService.refuseApply(no);
            return "success";
        }
        catch (Exception e)
        {
            return "fail";
        }
    }
    
    @ResponseBody
    @RequestMapping(value = "/apply", method = RequestMethod.POST)
    public String apply(@ModelAttribute Notify notify)
    {
        if (notifyService.isExist(notify.getUid(), notify.getFromUid(), ApplyStatus.INIT.value) || //
                notifyService.isExist(notify.getFromUid(), notify.getUid(), ApplyStatus.INIT.value))
        {
            return "repeat";
        }
        else
        {
            try
            {
                notifyService.apply(notify);
                return "success";
            }
            catch (Exception e)
            {
                return "fail";
            }
        }
    }
    
    private PageVo buildPageVo(int total, List<Notify> notifies)
    {
        PageVo pageVo = new PageVo();
        if (total == 0)
        {
            pageVo.setPages(1);
        }
        else
        {
            pageVo.setPages(total % PAGE_SIZE == 0 ? total / PAGE_SIZE : total / PAGE_SIZE + 1);
        }
        pageVo.setData(notifies);
        return pageVo;
    }
}
